﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace 公司CRM客户关系系统.Sale
{
    /// <summary>
    /// SaleAllot 的摘要说明
    /// </summary>
    public class SaleAllot : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            if (action == "paging")
            {
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                PagingList<V_Chances> list = BaseDAL.Paging<V_Chances>(pageSize, pageIndex, false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "select")
            {
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                //得到传过来的值
                string ChanName = context.Request["ChanName"];
                string ChanLinkMan = context.Request["ChanLinkMan"];
                string ChanCreateMan = context.Request["ChanCreateMan"];
                string ChanDueMan = context.Request["ChanDueMan"];
                //进行分页查询
                //执行查询
                SqlParameter[] pars = new SqlParameter[4];
                pars[0] = new SqlParameter("@ChanName", ChanName);
                pars[1] = new SqlParameter("@ChanLinkMan", ChanLinkMan);
                pars[2] = new SqlParameter("@ChanCreateMan", ChanCreateMan);
                pars[3] = new SqlParameter("@ChanDueMan", ChanDueMan);

                string whenStr = "ChanName like '%'+@ChanName+'%' and ChanLinkMan like '%'+@ChanLinkMan+'%' and ChanCreateName like '%'+@ChanCreateMan+'%' and ChanDueName like '%'+@ChanDueMan+'%'";
                PagingList<V_Chances> list = BaseDAL.Paging<V_Chances>(pageSize, pageIndex, whenStr, pars);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "ChanDueMan")
            {
                int UserID = int.Parse(context.Request.Form["UserID"]);//指派人的ID
                //获取要修改的 ChanID
                int ChanID = int.Parse(context.Request.Form["ChanID"]);
                //接收提交的数据
                Chances ch = new Chances();
                ch.ChanID = ChanID;
                ch.ChanDueMan = UserID;//修改指派人的ID
                //从数据库查出来的值(不需要修改)
                Chances chan = BaseDAL.Find<Chances>(ChanID);
                ch.ChanName = chan.ChanName;
                ch.ChanRate = chan.ChanRate;
                ch.ChanLinkMan = chan.ChanLinkMan;
                ch.ChanLinkTel = chan.ChanLinkTel;
                ch.ChanTitle = chan.ChanTitle;
                ch.ChanDesc = chan.ChanDesc;
                ch.ChanCreateDate = chan.ChanCreateDate;
                ch.ChanCreateMan = chan.ChanCreateMan;
                ch.ChanDueDate = chan.ChanDueDate;
                ch.ChanState = Convert.ToInt32(2);
                ch.ChanError = chan.ChanError;
                //修改数据库
                int r = BaseDAL.Update(ch);
                //返回结果
                context.Response.Write(r);

            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}